<template>
  <div class="photo-box">
    <!-- 顶部滑动 -->
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <a
            v-for="(item, index) in newsCate"
            :key="index"
            :class="['mui-control-item', item.id==0?'mui-active':'']"
            @click="getImgList(item.id)"
          >{{item.title}}</a>
        </div>
      </div>
    </div>
    <!-- 图片列表 v-lazy 懒加载-->
    <ul class="photo-list">
      <router-link :to="'/home/photoInfo/'+item.id" v-for="item in imgList" :key="item.id" tag="li">
        <img v-lazy="item.img_url">
      </router-link>
    </ul>
  </div>
</template>

<script>
//1.导入 mui 的JS文件
import mui from "../../lib/mui/js/mui.min.js";
import { Toast } from "mint-ui";

export default {
  data() {
    return {
      newsCate: [],
      imgList: [],
      pageIndex: 1, //默认页码
      pageSize: 10
    };
  },
  created() {
    this.getCateList();
    this.getImgList(0);
  },
  mounted() {
    //2.初始化MUI的滑动控件
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005
    });
  },
  methods: {
    getCateList() {
      this.$http.get("newsCate#!method=get").then(result => {
        if (result.status === 200) {
          this.newsCate = result.body.data;
          var allnews = {
            id: 0,
            title: "全部"
          };
          this.newsCate.unshift(allnews);
        } else {
          Toast("类别获取失败");
        }
      });
    },
    getImgList(cateId) {
      this.$http
        .get(
          "imgList?cateId=" +
            cateId +
            "page=" +
            this.pageIndex +
            "&pageSize=" +
            this.pageSize
        )
        .then(result => {
          if (result.status === 200) {
            this.imgList = result.body.data;
          } else {
            Toast("图片获取失败");
          }
        });
    }
  }
};
</script>

<style lang="scss" scoped>
* {
  touch-action: pan-y;
}

.photo-box {
  .mui-slider{
    background: #fff;
  }
  .photo-list {
    padding: 5px;
    li {
      margin: 0 auto 5px;
      padding: 0;
      text-align: center;
      background: #ccc;
      img[lazy="loading"] {
        width: 40px;
        height: 300px;
        margin: auto;
      }
      img {
        width: 100%;
        vertical-align: middle;
      }
    }
  }
}
</style>

